package com.hp.Interceptor;

import com.hp.annotation.Power;
import com.hp.context.Config;
import com.hp.entity.User;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.utils.JacksonUtil;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.List;

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String url = request.getRequestURL().toString();// 获取路径
        System.out.println("路径：" + url);
        if(url.contains(".html")||url.contains(".json")||url.contains("login.jsp")||url.contains("nopower.html")){
            return true;
        }


        HandlerMethod handlerMethod = (HandlerMethod) handler;//获取控制器方法

        Method method = handlerMethod.getMethod();//获取方法
   // AnnotationUtils.findAnnotation 查询方法上是否存在注解
        Power power = AnnotationUtils.findAnnotation(method, Power.class);//获取注解
        ResponseBody responseBody=AnnotationUtils.findAnnotation(method,ResponseBody.class);//获取


        User user = (User) request.getSession().getAttribute("user");//获取session
        List<com.hp.entity.Power> powerList = (List<com.hp.entity.Power>) request.getSession().getAttribute("powerList");


        if (power != null) {
            if (power.isPublic()) {//公共方法
                return true;
            } else {
                if (user == null||powerList==null||powerList.size()==0) {//没登入或超时
                  response.sendRedirect(request.getContextPath()+"/login.jsp");//return
                  return false;
                } else {
                    for (com.hp.entity.Power power1 : powerList) {
                           if(url.contains(power1.getMethod())){//有这个权限
                               return true;
                           }
                    }

                        if(responseBody==null){
                            response.sendRedirect(request.getContextPath()+"/nopower.html");
                            return false;
                        }else{
                            response.setCharacterEncoding("utf-8");
                            response.setContentType("application/json; charset=utf-8");
                            PrintWriter writer = response.getWriter();
                            SubmitResultInfo submitResult = ResultUtil.createSubmitResult(ResultUtil.createFail(Config.MESSAGE, 700, null));
                            writer.write(JacksonUtil.obj2Json(submitResult));
                            return false;
                        }


                }
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
